package com.hy.demo.controller;

import com.hy.demo.model.AccessToken;
import com.hy.demo.model.Ticket;
import com.hy.demo.model.UserTag;
import com.hy.demo.model.UserXmlVO;
import com.hy.demo.util.WeChatUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author cjl
 * @version 1.0
 * @since 2020-07-29 09:25
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Value("${weiXin.appID}")
    public String appId;

    @Value("${weiXin.appSecret}")
    public String appSecret;


    @RequestMapping("/createTag")
    public UserTag createTag(String name) throws IOException {
        // 获取accessToken
        AccessToken accessToken=WeChatUtil.getAccessToken(appId,appSecret);

        return WeChatUtil.createTag(accessToken.getAccessToken(),name);

    }

    @RequestMapping(value = "/sitemap.xml", produces = {"application/xml;charset=UTF-8", MediaType.TEXT_XML_VALUE})
    @ResponseBody
    public String test(){

        Ticket ticket=new Ticket();
        ticket.setErrCode(1);
        ticket.setErrMSg("cuowu");
        ticket.setTicket("123456");
        UserXmlVO userXmlVO=new UserXmlVO();
        userXmlVO.setName("root");
        userXmlVO.setTickets(ticket);
        System.out.println(userXmlVO);
        return userXmlVO.toString();
    }

    @GetMapping(value = "/test",produces=MediaType.TEXT_XML_VALUE)
    public Map<String, Object> getSomthing(){
        Map<String, Object> map = new HashMap<>();
        map.put("aaa", "aaaaa");
        return map;
    }
}
